﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{

	public class GroupchatDAO:IData<Groupchat> 
 	{
		public GroupchatDAO(String myconnecString) : base(myconnecString) { }

		protected override int InsertOnSubmit(ref Groupchat item, int t) {
			 SqlParameter p = new SqlParameter(); 
			 if( t == 0)
				p.Direction = ParameterDirection.Output;
			 p.ParameterName = "@" + Groupchat.c_id;
			 p.Value = item.id;
			 Object obj = item.id; 
			int i = connec.ExecuteNonQuery("sp_Groupchat", CommandType.StoredProcedure,p,ref obj,new SqlParameter("@flag", t),
				 new SqlParameter("@" + Groupchat.c_user_ids, item.user_ids), new SqlParameter("@" + Groupchat.c_contents, item.contents),
				 new SqlParameter("@" + Groupchat.c_createdate, item.createdate), new SqlParameter("@" + Groupchat.c_userid_send, item.userid_send),
				 new SqlParameter("@" + Groupchat.c_group_name, item.group_name));
			if(t == 0)
				item.id = Convert.ToInt64(obj);
			return i;
		}

		public override Groupchat GetItem(IDataReader dr)
		{
			Groupchat obj = new Groupchat();
			obj.id = Convert.ToInt64(dr[Groupchat.c_id]);
			obj.user_ids = Convert.ToString(dr[Groupchat.c_user_ids]);
			obj.contents = Convert.ToString(dr[Groupchat.c_contents]);
			obj.createdate = Convert.ToDateTime(dr[Groupchat.c_createdate]);
			obj.userid_send = Convert.ToInt64(dr[Groupchat.c_userid_send]);
			obj.group_name = Convert.ToString(dr[Groupchat.c_group_name]);
			return obj;
		}

		public override List<Groupchat> GetAll()
		{
			return GetAll("sp_GetAllGroupchat", CommandType.StoredProcedure);
		}

		public override Groupchat GetItemById(object id)
		{
			return GetAll().SingleOrDefault(p => p.id == Convert.ToInt64(id));
		}
		public override Groupchat GetItemById(Groupchat item){
			throw new NotImplementedException();
		}

	}

 }
